package br.trainee.web.componentes;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

import br.trainee.web.util.ResponseWrapper;

public class FooterTag extends BodyTagSupport{

	private String footerClass;
	
	@Override
	public int doEndTag() throws JspException {

		String corpo = getBodyContent().getString();
		
		JspWriter saida = pageContext.getOut();
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
		
		request.setAttribute("id", id);
		request.setAttribute("footerClass", footerClass);
		request.setAttribute("body", corpo);
		
		ResponseWrapper responseWrapper = new ResponseWrapper(response);
		
		RequestDispatcher req = request.getRequestDispatcher("/resources/componentes-jsp/footer.jsp");
		
		try {
			
			//faz a mesma função do forward, so que podendo fazer alterações apos enviar a requisição
			req.include(request, responseWrapper);
			String conteudoSaida = responseWrapper.getString();
			saida.println(conteudoSaida);
			
		} catch (ServletException | IOException e1) {

			e1.printStackTrace();
		}
		
		return super.doEndTag();
	}

	public String getFooterClass() {
		return footerClass;
	}

	public void setFooterClass(String footerClass) {
		this.footerClass = footerClass;
	}
	
	
	
}
